﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PFI.ADCConv;
using PFI.DAConv;
using PFI.SCHVN;
using PFI.RegisterDevice;
using Utils.RamDevice;
using Algorithm.CalibrationWeights;

namespace Utils.GlobalGathering
{
    public class GlobalUnit
    {
        public bool IsWeightsByHands { get; set; }
        public ADC adcInst { get; set; }
        public DAC dacInst { get; set; }

        public GlobalUnit()
        {
            IsWeightsByHands = false;
        }

        #region ADC
        
        public void SetAdcNewWeights()
        {
            if (IsWeightsByHands)
            {
                SetAdcWeightsByHand();
                this.adcInst.isCalibrated = false;
                RAM.Weights.InitRAMWeights(this.adcInst.dac.schvn);
            }
            else
            {
                adcInst.SetNewWeights();
                this.adcInst.isCalibrated = false;
                RAM.Weights.InitRAMWeights(this.adcInst.dac.schvn);
            }
        }
        public void CalibrationsOfADC()
        {
            if (adcInst.reg is SCHVNRegister)
            {
                CalibrateAdcWeights(StrategyEnums.First);
                CalibrateAdcTrack();
            }
        }
        private void CalibrateAdcTrack()
        {
            CalibratorOfTrackingCombinations calibrator = new CalibratorOfTrackingCombinations(this.adcInst);
            calibrator.CalibrateTrackingCombinationsFirstStrategy();
            this.adcInst.isCalibrated = true;
        }
        private void CalibrateAdcWeights(StrategyEnums strategyType)
        {
            CalibratorOfWeights calibrator = new CalibratorOfWeights(this.adcInst);
            calibrator.CalibrateWeights(strategyType);
        }
        public void SetAdcWeightsByHand()
        { 
            this.adcInst.dac.schvn.SetRealWeigths(WeightsByHand_Numbers.Get_Weights());
        }

        #endregion ADC

        #region DAC
        public void SetDACNewWeights()
        {
            if (IsWeightsByHands)
            {
                SetDacWeightsByHand();
                this.dacInst.isCalibrated = false;
                RAM.Weights.InitRAMWeights(this.dacInst.schvn);
            }
            else
            {
                this.dacInst.schvn.ReCountRealWeigths();
                this.dacInst.isCalibrated = false;
                RAM.Weights.InitRAMWeights(this.dacInst.schvn);
            }
            
        }
        public void CalibrationsOfDAC(StrategyEnums strategyType)
        {
            this.adcInst = new ADC(this.dacInst.schvn);
            CalibrateAdcWeights(strategyType);
            CalibrateAdcTrack();
            dacInst.isCalibrated = true;
        }
        public void SetDacWeightsByHand()
        {
            this.dacInst.schvn.SetRealWeigths(WeightsByHand_Numbers.Get_Weights());
            dacInst.isCalibrated = false;
        }
        #endregion DAC
    }
}
